package com.huayue.dao;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.huayue.entity.RoomChange;
import com.huayue.util.DateHelper;
import com.mysql.DBHelper;

/**
 * 换房
 */
@Component
public class RoomChangeDao {

    public Map<String, Object> findOneRoom(RoomChange roomChange) {
        return DBHelper
            .executeQuery("select r.id,r.name,rt.price from room r LEFT JOIN room_type rt on r.room_type_id = rt.id \n"
                + "where r.is_deleted=0 and r.id=? ", roomChange.getRoom_type_id())
            .get(0);
    }

    public List<Map<String, Object>> findType() {
        return DBHelper.executeQuery(
            "select rt.id,rt.name,rt.price from room r LEFT JOIN room_type rt on r.room_type_id = rt.id \n"
                + "where r.is_deleted=0 GROUP BY room_type_id");
    }

    public List<Map<String, Object>> findTypeRoom(RoomChange roomChange) {
        return DBHelper.executeQuery("select * from room where room_type_id=? and is_deleted=0",
            roomChange.getType_room_id());
    }

    public int findOverDaysByRoomId(Integer roomId) {
        List<Map<String, Object>> list =
            DBHelper.executeQuery("select out_time from room_status where room_id=?", roomId);
        // 计算时间
        Date DBDate = DateHelper.StringToDateShort(list.get(0).get("out_time").toString());
        int i = DateHelper.getDateRangeNumber(new Date(), DBDate);
        if (i == 0) {
            return 1;
        }
        return i;
    }

    /**
     * 修改新房间状态
     * 
     * @param roomChange
     * @return
     */
    public int updateNewRoom(RoomChange roomChange) {
        // 将老房间中的客户信息转至新房间 并改变新房间状态
        String sql = "update room_status set status_id = 2,\n"
            + "customer_id = (select c.id from(select customer_id id from room_status where room_id=?)c),\n"
            + "in_time=(select ie.in_time from(select in_time from room_status where room_id=?)ie),\n"
            + "out_time=(select b.out_time from (select out_time from room_status where room_id=?)b) where room_id=?";
        int i = DBHelper.executeUpdate(sql, roomChange.getOldId(), roomChange.getOldId(), roomChange.getOldId(),
            roomChange.getNewId());
        // 将顾客信息转至新房间
        String sqlcustomer = "update customer set room_id=? where room_id=?";
        i += DBHelper.executeUpdate(sqlcustomer, roomChange.getOldId(), roomChange.getNewId());
        return i;
    }

    /**
     * 修改原房间状态
     * 
     * @param roomChange
     * @return
     */
    public int updateOldRoom(RoomChange roomChange) {
        return DBHelper.executeUpdate(
            "update room_status set status_id = ?,in_time=null,out_time=null,customer_id=null where room_id=?",
            roomChange.getOldStatus(), roomChange.getOldId());
    }

    public Map<String, Object> findRoomReal(RoomChange roomChange) {
        String sql = "SELECT rr.room_real_order_id rid,rr.customer_id cid from room_real_order_details rr "
            + "LEFT JOIN room_real_order r on rr.room_real_order_id = r.id "
            + "where rr.room_id = ? order by rr.id desc limit 0,1";
        return DBHelper.executeQuery(sql, roomChange.getOldId()).get(0);
    }

    // 添加使用新房价时换房详情状态
    public int insertNewRealDetails(int rid, int cid, RoomChange roomChange) {
        return DBHelper.executeUpdate(
            "INSERT INTO room_real_order_details (room_real_order_id,customer_id,room_type_id,room_id,price,is_status) VALUES (?,?,?,?,?,2)",
            rid, cid, roomChange.getRoom_type_id(), roomChange.getNewId(), roomChange.getNewPrice());
    }

    // 添加不使用新房价时换房详情状态
    public int insertOldRealDetails(int rid, int cid, RoomChange roomChange) {
        // 需根据新房价查询到上一次房价
        int i = DBHelper.executeUpdate(
            "INSERT INTO room_real_order_details (room_real_order_id,customer_id,room_type_id,room_id,price,is_status) "
                + " VALUES (?,?,?,?,?,2)",
            rid, cid, roomChange.getRoom_type_id(), roomChange.getNewId(), roomChange.getOldPrice());
        return i;
    }

    // 根据房间id查询最初的订单id
    public int findGenRoomDetailByRoomId(Integer roomid) {
        // 查询到订单id -->订单id正序排第一条数据
        return Integer.valueOf(DBHelper
            .executeQuery("select id from room_real_order_details where room_real_order_id="
                + "(select d.room_real_order_id from "
                + "(select rrod.room_real_order_id from room_real_order_details rrod "
                + "where room_id=? order by id desc limit 0,1)d) order by id asc limit 0,1 ", roomid)
            .get(0).get("id").toString());
    }

    // 修改原房间详情状态
    public int updateRealDetails(Integer rid, RoomChange roomChange) {
        // 将原来的预定款+=补缴的钱
        // 原房间id
        DBHelper.executeUpdate("update room_real_order_details set total_price=total_price+? where id=?",
            roomChange.getAddPrice(), findGenRoomDetailByRoomId(roomChange.getOldId()));

        return DBHelper.executeUpdate(
            "update room_real_order_details set is_status=1,change_id=? where room_id=? and room_real_order_id = ?",
            roomChange.getNewId(), roomChange.getOldId(), rid);
    }

    // 将原房间押金转至新房间
    public int updateRoomDeposit(Integer orderId, Integer oldRoomId, Integer newRoomId) {
        String sql = "update room_deposit set room_id=? where room_real_order_id=? and room_id = ?";
        return DBHelper.executeUpdate(sql, newRoomId, orderId, oldRoomId);
    }
}
